package com.mfnote.util;

import java.util.HashMap;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.mfnote.view.R;

/**
 * Receive the broadcast and start the activity that will show the alarm
 * 
 * @author ZHF
 * 
 */
public class MyReceiver extends BroadcastReceiver {
	private Context context = null;
	private HashMap<String, Object> map = null;
	private int requestCode = 0;
	/**
	 * called when the BroadcastReceiver is receiving an Intent broadcast.
	 */
	@Override
	public void onReceive(Context context, Intent intent) {

		/* start another activity - MyAlarm to display the alarm */
		Log.d("action", intent.getAction());
		this.context = context;
		Intent intent_action = null;
		String content = "";
		String action = intent.getAction();
		map = (HashMap<String, Object>) intent.getSerializableExtra("map");
		requestCode = intent.getIntExtra("requestCode", -1);
		Log.d("requestCode", requestCode+"---");
		if ("com.mfnote.view.handwriting".equals(action)) {
			// 收到来自手绘的消息
			intent_action = new Intent(context,
					com.mfnote.view.hand.HandWritingActivity.class); // 点击启动该应用
			content = "手绘中有您的重要信息，点击查看";
		} else if ("com.mfnote.view".equals(action)) {
			// 收到来自记事本的消息
			intent_action = new Intent(context,
					com.mfnote.view.write.NewWriteActivity.class); // 点击启动该应用
			content = "记事本中有您的重要信息，点击查看";
		}
		intent_action.putExtra("map", map);
		intent_action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		openNotification(intent_action, content);

		// Intent it=new Intent(context,MyNotification.class);
		// it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(it);
		// 收到广播后启动Activity,发送Notification出去
		// intent必须加上Intent.FLAG_ACTIVITY_NEW_TASK flag

	}

	/** 弹出通知消息 **/
	private void openNotification(Intent intent, String content) {
		System.out.println("收到广播");
		// 弹出消息提醒
		NotificationManager manager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个通知的管理类

		// Notification notification = new
		// Notification(R.drawable.app_icon,"来自易事记事本消息",
		// System.currentTimeMillis());
		// PendingIntent contentIntent =
		// PendingIntent.getActivity(context,0,intent,0);
		// notification.setLatestEventInfo(context, "My Title", "My Content",
		// contentIntent);
		// manager.notify(R.drawable.app_icon, notification);

		// 3.0以后有Notification.Builder
		//Notification.Builder builder = new Notification.Builder(context);

		// Intent intent2 = new Intent(Intent.ACTION_DIAL); //点击进入拨号界面
		// String phone = NewWriteActivity.keywords;
		// Uri uri = Uri.parse("tel:"+ phone);
		// Intent intent2 = new Intent(Intent.ACTION_DIAL,uri); //点击进入拨号界面
		// intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //添加一个新的任务进去
		// context.startActivity(intent2);

		Notification notification = new Notification(R.drawable.app_icon,"来自MFNote消息",System.currentTimeMillis());
		PendingIntent conIntent = PendingIntent.getActivity(context, requestCode, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(context, "来自MFNote消息", content, conIntent);
		notification.defaults = Notification.DEFAULT_ALL;
		
//		builder.setContentIntent(conIntent);
//		builder.setContentTitle("来自MFNote消息");
//		builder.setContentText(content);
//		builder.setTicker("来自MFNote消息！");
//		builder.setSmallIcon(R.drawable.app_icon);
//		builder.setDefaults(Notification.DEFAULT_ALL); // 闪光，铃声、震动 默认
//
//		Notification notification = builder.getNotification();
		// 点击通知后消失的代码
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		manager.notify(R.drawable.app_icon, notification);
		
		//回收requestCode
		RandomUtils.recycleRequestCode(requestCode);
	}
}